package com.mi.rof.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.mi.rof.engineer.entity.RepairOrderStates;
import com.mi.rof.entity.enums.InventoryApplyEnum;
import com.mi.rof.entity.enums.PaymentStateEnum;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author zwq
 * 自动补充默认值
 */
@Component
@Configuration
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "isDeleted", Integer.class, 0);
        this.strictInsertFill(metaObject, "state", Integer.class, RepairOrderStates.ORDER_SUBMITTED.getCode());
        this.strictInsertFill(metaObject, "paymentState", Integer.class, PaymentStateEnum.APPLIED.getCode());
        this.strictInsertFill(metaObject, "inventoryState", Integer.class, InventoryApplyEnum.APPLIED.getCode());
        this.setFieldValByName("createdTime", new Date(), metaObject);
        this.setFieldValByName("updatedTime", new Date(), metaObject);
        this.setFieldValByName("paymentTime", new Date(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updatedTime", new Date(), metaObject);
    }
}
